/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.gwt.form.client.value.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;

/**
 * implementation of {@link DataIdentifier}.
 */
public class DataIdentifierImpl
    implements DataIdentifier
{

  /**
   * the identifier of the data.
   */
  private String identifier = null;

  /**
   * the identifiers of the parent of the data. The first element is the
   * identifier of the parent, the next the identifier of the parent of the
   * parent, etc.
   */
  private List<String>parentIdentifiers = new ArrayList<String>();



  /**
   * constructor.
   * 
   * @param identifier
   *          the identifier of the data
   */
  public DataIdentifierImpl(Object identifier)
  {
    setIdentifier(identifier);
  }



  /**
   * constructor.
   */
  public DataIdentifierImpl()
  {
    this(null);
  }



  /**
   * getter for the identifier property.
   * 
   * @return the identifier
   */
  public String getIdentifier()
  {
    return identifier;
  }



  /**
   * setter for the identifier property.
   * 
   * @param identifier
   *          the identifier to set
   */
  public void setIdentifier(Object identifier)
  {
    if (identifier == null) {
      this.identifier = null;
    }
    else {
      this.identifier = String.valueOf(identifier);
    }
  }



  /**
   * returns a {@link DataIdentifier} for a child of the element described by
   * the current {@link DataIdentifier}.
   * 
   * @param childIdentifier
   *          the identifier of the child
   * @return a {@link DataIdentifier} for a child of the element described by
   *         the current {@link DataIdentifier}
   */
  public DataIdentifier getChildDataIdentifier(Object childIdentifier)
  {
    DataIdentifierImpl result = new DataIdentifierImpl(childIdentifier);
    List<String> childParentIdentifiers = new ArrayList<String>(getParentIdentifiers());
    childParentIdentifiers.add(0, getIdentifier());
    result.setParentIdentifiers(childParentIdentifiers);
    return result;
  }



  /**
   * returns a {@link DataIdentifier} for a child of the element given in
   * parameter. If the element is <code>null</code> a {@link DataIdentifier}
   * for the child with no parent is returned.
   * 
   * @param parentDataIdentifier
   *          the parent {@link DataIdentifier}
   * @param childIdentifier
   *          the identifier of the child
   * @return a {@link DataIdentifier} for a child of the element given in
   *         parameter
   */
  // public static DataIdentifier getChildDataIdentifier(DataIdentifier
  // parentDataIdentifier, Object childIdentifier)
  // {
  // if (parentDataIdentifier == null) {
  // return new DataIdentifierImpl(childIdentifier);
  // }
  // return parentDataIdentifier.getChildDataIdentifier(childIdentifier);
  // }

  /**
   * getter for the parentIdentifiers property.
   * 
   * @return the parentIdentifiers
   */
  public List<String> getParentIdentifiers()
  {
    return parentIdentifiers;
  }



  /**
   * setter for the parentIdentifiers property.
   * 
   * @param parentIdentifiers
   *          the parentIdentifiers to set
   */
  public void setParentIdentifiers(List<String> parentIdentifiers)
  {
    this.parentIdentifiers = parentIdentifiers;
  }
}
